﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Input;
using CLEXA.AdminClient.View;
using CLEXA.ClientTools.Collections;
using CLEXA.DataContracts;

namespace CLEXA.AdminClient.ViewModel
{
    internal sealed class UserListVM : VMBase
    {
        private readonly IFacade facade;
        private readonly ListBinding<int, UserData, UserReadVM> users;
        private UserReadVM selectedUser;
        private RelayCommand newUser;
        private RelayCommand editUser;
        private RelayCommand deleteUser;

        public IEnumerable<UserReadVM> Users
        {
            get { return users.ObservableCollection; }
        }

        public UserReadVM SelectedUser
        {
            get { return selectedUser; }
            set
            {
                if (selectedUser != value)
                {
                    selectedUser = value;
                    RaisePropertyChanged("SelectedUser");
                }
            }
        }

        public ICommand NewUser
        {
            get
            {
                if (newUser == null)
                {
                    newUser = new RelayCommand(DoNewUser);
                }
                return newUser;
            }
        }

        public ICommand EditUser
        {
            get
            {
                if (editUser == null)
                {
                    editUser = new RelayCommand(DoEditUser, CanEditUser);
                }
                return editUser;
            }
        }

        public ICommand DeleteUser
        {
            get
            {
                if (deleteUser == null)
                {
                    deleteUser = new RelayCommand(DoDeleteUser, CanDeleteUser);
                }
                return deleteUser;
            }
        }

        public UserListVM(IFacade facade)
        {
            this.facade = facade;
            this.users = new ListBinding<int, UserData, UserReadVM>(facade.Users, p => new UserReadVM(p.UserId, p.Username, p.IsActive), UserReadVM.IdentityComparer);
        }

        private void DoNewUser()
        {
            facade.ShowUserCreateDialog();
        }

        private void DoEditUser()
        {
            facade.ShowUserEditDialog(selectedUser.UserId);
        }

        private bool CanEditUser()
        {
            return SelectedUser != null;
        }

        private void DoDeleteUser()
        {
            if (facade.ShowYesOrNoDialog(string.Format("Are you sure you want to delete user {0}?", selectedUser.Username)))
            {
                facade.UserDelete(SelectedUser.UserId);
            }
        }

        private bool CanDeleteUser()
        {
            return SelectedUser != null;
        }
    }
}
